// 地图
Ext.define('qjtravel.view.PopMap', {
    extend: 'Ext.Container',
    xtype: 'popmap',


    config: {
        layout: 'vbox',
        listeners: {
            activate: function () {
                this.initBaidumap();
            }
        },
        items: [
            {
                xtype: 'toolbar',
                docked: 'top',
                items: [
                    {
                        xtype: 'button',
                        text: '返回',
                        handler: function () {
                            var backid = Ext.Viewport.getInnerItems().length - 2;
                            Ext.Viewport.remove(this.up('popmap'), true);
                            Ext.Viewport.setActiveItem(backid);
                        }
                    },{
                        xtype: "spacer"
                    },
                    {
                        xtype: 'button',
                        text: '导航',
                        handler: function () {
                            Ext.Viewport.mask({xtype: 'loadmask',message: '加载数据中...' });
                            this.up('popmap').driving();
                         }
                    }
                ]
            },
            {
                xtype: 'container',
                id: "popmap",
                html: "正在加载地图...",
                flex: 1
            }
        ], listeners: {
            back: function () {
                var backid = Ext.Viewport.getInnerItems().length - 2;
                Ext.Viewport.remove(Ext.Viewport.getActiveItem(), true);
                Ext.Viewport.setActiveItem(backid);
            }
        }
    },

    isMap: false,
    baidumap: null,
    area : '',

    initBaidumap: function () {
        if (!this.isMap && BMap != 'undefined') {
            this.isMap = true; 
            var map = new BMap.Map('popmap'); 
            map.addControl(new BMap.NavigationControl());
            map.addControl(new BMap.ScaleControl());  
            var point = new BMap.Point(108.953451, 34.265773);
            map.centerAndZoom(point, 16); 
            map.addOverlay(new BMap.Marker(point)); 
            map.addEventListener("tilesloaded", function(){
                var alllink= Ext.query('#popmap .BMap_cpyCtrl');
                alllink.length > 0 ?alllink[0].style.display='none' : "";
                for(var ims=0;ims<Ext.query('#popmap a').length;ims++){
                    var links=Ext.query('#popmap a')[ims].href;
                    if(links.indexOf('baidu')!=-1){
                        Ext.query('#popmap a')[ims].href = "javascript:void(0);";
                    }

                    if(Ext.query('#popmap a')[ims].innerHTML.indexOf('详情')!=-1){
                        Ext.query('#popmap a')[ims].innerHTML='';
                    }
                }
            });
            this.baidumap = map;

        }
    },
    driving: function(){
        var bmap = this.baidumap;
        var area = this.area;
        var onSuccess = function(position) { 
        var gpsPoint = new BMap.Point(
            position.coords.longitude,
            position.coords.latitude
            );

        BMap.Convertor.translate(gpsPoint,0,function (point){
            bmap.clearOverlays(); 
            var driving = new BMap.DrivingRoute(bmap, {renderOptions:{map: bmap, autoViewport: true}});
            driving.search(point, area);  

        });
          Ext.Viewport.unmask();
        };

        // onError Callback receives a PositionError object
        //
        function onError(error) {
            cordova.exec(function (message) { }, function (error) {}, "Messages", "showMsg", [error.message]);
            Ext.Viewport.unmask();
        } 
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
    },
    search: function (area) {
        if (!this.isMap && BMap != 'undefined') {
            this.initBaidumap();
        } 
        var local = new BMap.LocalSearch("西安市", {
            renderOptions: {
                map: this.baidumap
            }
        });
        this.area = area;
        local.enableFirstResultSelection();
        local.setInfoHtmlSetCallback(function(poi) {
            var aLink = Ext.query('.BMap_pop a');
            var url = aLink[0].href;
            aLink[0].href = "";
            aLink[0].innerHTML = '';
        }); 
        local.search(area);

    }
});